package servlet;

import db.DB;
import model.User;
import model.permission;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;

@WebServlet("/updatepwd")//访问的时候给一个地址,高于3.0版本的包才能这么配地址
public class updatepwd extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setHeader("content-type","text/html;charset=utf-8");
        String oldpwd=req.getParameter("oldpassward");
        String newpwd=req.getParameter("newpassward");
        HttpSession session=req.getSession();
        DB DB=new DB();
        User us=(User)session.getAttribute("user");
        String message = null; //定义message变量
        if(oldpwd.equals(us.getPassword())){
            if(DB.updatepwd(newpwd,us.getId())){
                resp.getWriter().print("<script language='javascript'>" +
                    "alert('修改成功！');" +
                    "window.location.href='updatePwd.jsp';</script>')");}
            else{
                resp.getWriter().print("<script language='javascript'>" +
                        "alert('修改失败，请重新尝试！');" +
                        "window.location.href='updatePwd.jsp';</script>')");
            }
        }
        else {
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('您输入的原密码有误，请重新输入！');" +
                    "window.location.href='updatePwd.jsp';</script>')");
//            message = "账号错误！！";
//            session.setAttribute("message",message);
//            resp.sendRedirect("login.jsp");
        }
    }

}
